package com.my.blog.website.config;

import com.my.blog.website.modal.LoggerEntity;
import com.my.blog.website.utils.LoggerUtils;
import com.my.blog.website.utils.ServletUtils;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * desc
 *
 * @author leihz
 * @date 2017/6/23 12:35
 */
@Component
@Aspect
public class LogAspectJWeave {

    @Pointcut("execution(public * com.my.blog.website.controller.*.*(..))")
    public void log() {}

    @Before("@annotation(es)")
    public void interAnnotation(Easy es){
        System.out.println(this.getClass().getSimpleName()+"  拦截了注解方法===>"+es.value());

        HttpServletRequest request =  ServletUtils.getRequest();
        //创建日志实体
        LoggerEntity logger = new LoggerEntity();
        //获取请求sessionId
        String sessionId = request.getRequestedSessionId();
        //请求路径
        String url = request.getRequestURI();
        //获取请求参数信息
//        String paramData = JSON.toJSONString(request.getParameterMap(),
//                SerializerFeature.DisableCircularReferenceDetect,
//                SerializerFeature.WriteMapNullValue);
        //设置客户端ip
        logger.setClientIp(LoggerUtils.getCliectIp(request));
        //设置请求方法
        logger.setMethod(request.getMethod());
        //设置请求类型（json|普通请求）
        logger.setType(LoggerUtils.getRequestType(request));
        //设置请求参数内容json字符串
//        logger.setParamData(paramData);
        //设置请求地址
        logger.setUri(url);
        //设置sessionId
        logger.setSessionId(sessionId);
        //设置请求开始时间
        //request.setAttribute(LOGGER_SEND_TIME,System.currentTimeMillis());
        //设置请求实体到request内，方面afterCompletion方法调用
        //request.setAttribute(LOGGER_ENTITY,logger);



    }



}
